﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HF.Business.Sale;
using HF.Framework.Web;
using HF.Business.Common;
using System.IO;

public partial class ProductEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            initData();
    }
    private void initData()
    {
        if (Request["Product_Code"] != null)
        {
            btnAdd.Visible = false; 
            Product p = new Product();
            p.GetProductInfo(Request["Product_Code"].ToString());
            tbxProductCode.Text = p.Product_Code;
            tbxProductName.Text = p.Product_Caption;
            tbxProductType.Text = p.Product_Type;
            tbxProductUnit.Text = p.Product_Unit;
            tbxProductPrice.Text = p.Product_Price.ToString();
            tbxProductMemo.Text = p.Product_Memo;
            tbxImageUrl1.Text = p.ImageUrl1;
            tbxImageUrl2.Text = p.ImageUrl2;
            tbxImageUrl3.Text = p.ImageUrl3;
            tbxImageUrl4.Text = p.ImageUrl4;

            Image1.ImageUrl = p.ImageUrl1;
            Image2.ImageUrl = p.ImageUrl2;
            Image3.ImageUrl = p.ImageUrl3;
            Image4.ImageUrl = p.ImageUrl4;

        }
        else
        {
            btnModify.Visible = false;
            tbxProductCode.Text = BusinessComm.GetDeciCode("PRODUCT",6);
            
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (Product.ProductExists(tbxProductCode.Text))
        {
            WorkDialog.ShowMessage(this,tbxProductCode.Text+"该编号的产品已存在,编号不能重复!!");
            return;
        }
        if (tbxProductCode.Text.Length <= 0)
        {
            WorkDialog.ShowMessage(this, "编号不能为空!!");
            return;
        }
        try
        {
            Product p = new Product();
            p.Product_Code = tbxProductCode.Text;
            p.Product_Caption = tbxProductName.Text;
            p.Product_Type = tbxProductType.Text;
            p.Product_Unit = tbxProductUnit.Text;
            p.Product_Price = Convert.ToDouble(tbxProductPrice.Text);
            p.Product_Memo = tbxProductMemo.Text;
            p.ImageUrl1 = tbxImageUrl1.Text;
            p.ImageUrl2 = tbxImageUrl2.Text;
            p.ImageUrl3 = tbxImageUrl3.Text;
            p.ImageUrl4 = tbxImageUrl4.Text;
            p.Insert();
            WorkDialog.ShowMessage(this, "增加成功!");
        }
        catch (Exception ex)
        {
            WorkDialog.ShowMessage(this,"增加错误:"+ex.Message.ToString());
        }
    }
    
    protected void btnModify_Click(object sender, EventArgs e)
    {
        if (tbxProductCode.Text.Length <= 0)
        {
            WorkDialog.ShowMessage(this, "编号不能为空!!");
            return;
        }
        try
        {
            Product p = new Product();
            p.Product_Code = tbxProductCode.Text;
            p.Product_Caption = tbxProductName.Text;
            p.Product_Type = tbxProductType.Text;
            p.Product_Unit = tbxProductUnit.Text;
            p.Product_Price = Convert.ToDouble(tbxProductPrice.Text);
            p.Product_Memo = tbxProductMemo.Text;
            p.ImageUrl1 = tbxImageUrl1.Text;
            p.ImageUrl2 = tbxImageUrl2.Text;
            p.ImageUrl3 = tbxImageUrl3.Text;
            p.ImageUrl4 = tbxImageUrl4.Text;
            p.Update();
            WorkDialog.ShowMessage(this, "修改成功!");
        }
        catch (Exception ex)
        {
            WorkDialog.ShowMessage(this, "修改错误:" + ex.Message.ToString());
        }
    }
    private void upImagefile(FileUpload fu,TextBox tbx,Image img)
    {
        if (fu.PostedFile.ContentLength > 0)
        {
            //设定上传文件的保存路径
            string strSaveDir = "picimage/";
            string strName = fu.PostedFile.FileName;
            string strfilename = strSaveDir + Path.GetFileName(strName);
            string type = Path.GetExtension(strName);//获得文件扩展名
            type = type.ToLower();
            if (type == ".jpg" || type == ".png" || type == ".bmp" || type == ".gif" || type == ".jpeg")
            {
                string filenamepath = Server.MapPath(strfilename);
                fu.PostedFile.SaveAs(filenamepath);
                //得到这个文件的相关属性:文件名,文件类型,文件大小
                tbx.Text = strfilename;
                img.ImageUrl = strfilename;
            }
        }
    }
    protected void ButtonConfirm_Click(object sender, EventArgs e)
    {
        upImagefile(fuImage1, tbxImageUrl1, Image1);

    }
    protected void ButtonConfirm0_Click(object sender, EventArgs e)
    {
        upImagefile(fuImage2, tbxImageUrl2, Image2);
    }
    protected void ButtonConfirm1_Click(object sender, EventArgs e)
    {
        upImagefile(fuImage3, tbxImageUrl3, Image3);
    }
    protected void ButtonConfirm2_Click(object sender, EventArgs e)
    {
        upImagefile(fuImage4, tbxImageUrl4, Image4);
    }
    protected void btnModify0_Click(object sender, EventArgs e)
    {
        
    }
}
